/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view.Papers;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ListSelectionModel;
import javax.swing.JLabel;

import view.MainFrame;
import view.userProfile.AuthorView;
import java.awt.GridLayout;

/**
 * Allows the author to edit, resubmit, or delete their paper. 
 *   Also allows the author to download reviews of their paper.
 *   
 * @author Kon, Thang, Emily
 * @version March 20, 2014
 */
@SuppressWarnings("serial")
public class EditPapers extends JPanel{
	
	/** The frame associated with the panel. */
	private MainFrame my_frame;

	/** The conference the author is currently working within. */
	private String current_conference;

	/** The model used to display the papers of the author. */
	private DefaultListModel paperModel = new DefaultListModel();
	
	/** A JList of the papers an author has submitted. */
	private JList paperlist;
	
	/**
	 * Constructs the panel for the author to edit papers and download reviews.
	 * 
	 * @param the_frame the frame the panel is to be associated with
	 * @author Kon 
	 * Editor: Thang, Emily
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EditPapers(MainFrame the_frame, String the_conference){
		super();
		my_frame = the_frame;
		current_conference = the_conference;
		
		
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setBackground(Color.WHITE);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(221, 160, 221));
		panel.add(panel_1);
		panel_1.setLayout(null);

		ArrayList<String> authorPaperList = my_frame.db.getAuthorPapersForConf(my_frame.currentUser[0][0], current_conference);
		final String[] authorPaperArray = new String[authorPaperList.size()];
		int i = 0;
		for (int j = 0; j < authorPaperList.size(); j++) {
			System.out.println(authorPaperList.get(j));
			if (authorPaperList.get(j) != null) {
				paperModel.addElement(authorPaperList.get(j));
				System.out.println(authorPaperList.get(j));
				i++;
			}
		}
		
		paperlist = new JList(paperModel);
		paperlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		paperlist.setFont(new Font("Tahoma", Font.PLAIN, 15));
		paperlist.setBounds(10, 34, 229, 303);
		
		panel_1.add(paperlist);
		
		JLabel lblTitles = new JLabel("Papers");
		lblTitles.setFont(new Font("Tahoma", Font.ITALIC, 18));
		lblTitles.setBounds(71, 6, 59, 29);
		panel_1.add(lblTitles);
		
		ArrayList<String> conferenceList = my_frame.db.getConferences();
		String[] conferenceArray = new String[conferenceList.size() + 1];
		conferenceArray[0] = null;
		for(int j = 0; j < conferenceList.size(); j++) {
			conferenceArray[j + 1] = conferenceList.get(j);
		}
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(new Color(221, 160, 221));
		panel.add(panel_3);
		panel_3.setLayout(null);
		
		JButton btnBack = new JButton("<- Back");
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				my_frame.setNewPanel(new AuthorView(my_frame));
				my_frame.revalidate();
				my_frame.repaint();
			}
		});
		btnBack.setBounds(39, 240, 167, 33);
		panel_3.add(btnBack);
		
		JButton resubmitPaper = new JButton("Resubmit");
		resubmitPaper.setBounds(39, 45, 167, 33);
		panel_3.add(resubmitPaper);
		resubmitPaper.setFont(new Font("Tahoma", Font.PLAIN, 15));
		resubmitPaper.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (paperlist.isSelectionEmpty() 
						|| paperlist.getSelectedValue() == null 
						|| paperlist == null){
					JOptionPane.showMessageDialog(my_frame, "Please select a paper!", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else {
					// do the resubmit paper.
					File file = my_frame.chooseFileOpen();
					
					String paperName = paperlist.getSelectedValue().toString();
					try {
						CopyDocuments.copy(new File("Paper/"+ paperName),file);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					
					//Nothing needs done with data base.
					JOptionPane.showMessageDialog(my_frame, "Sucessfully ReSubmitted Paper!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		
		JButton unSubmit = new JButton("Unsubmit ");
		unSubmit.setBounds(39, 117, 167, 33);
		panel_3.add(unSubmit);
		unSubmit.setFont(new Font("Tahoma", Font.PLAIN, 15));
		unSubmit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (paperlist.isSelectionEmpty() 
						|| paperlist.getSelectedValue() == null 
						|| paperlist == null){
					JOptionPane.showMessageDialog(my_frame, "Please select a paper!", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else {
					// do the unsubmit paper.
					
					String paperName = paperlist.getSelectedValue().toString();
					paperModel.remove(paperlist.getSelectedIndex());
					System.out.print(paperName);
					if (paperName != null) {
						String authorName = my_frame.db.getAuthor(paperName, current_conference);
						String programChair = my_frame.db.getProgChair(current_conference);
						String subChair = my_frame.db.getSubChairForPaper(paperName, current_conference, authorName);
						String reviewer = my_frame.db.getReviewer(paperName, current_conference, authorName);
						my_frame.db.deletePaper(authorName, current_conference, paperName, programChair, subChair, reviewer);
						JOptionPane.showMessageDialog(my_frame,  "Sucessfully deleted paper!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					}
					
				}
			}
		});
		
		JButton downloadReview = new JButton("Download Review");
		downloadReview.setBounds(39, 179, 167, 33);
		panel_3.add(downloadReview);
		downloadReview.setFont(new Font("Tahoma", Font.PLAIN, 15));
		downloadReview.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (paperlist.isSelectionEmpty()
							|| paperlist.getSelectedValue() == null 
							|| paperlist == null) {
						JOptionPane.showMessageDialog(my_frame, "Please select a paper!", "Error!", JOptionPane.ERROR_MESSAGE);
				} else {
					String paperName = (String)paperlist.getSelectedValue();
					String authorName = my_frame.db.getAuthor(paperName, current_conference);
					ArrayList<String> paperReviews = my_frame.db.getReview(authorName, current_conference, paperName);
					if(paperReviews.isEmpty()) {
						JOptionPane.showMessageDialog(null, "This paper has no reviews!");
					} {
						for(int i=0;i<paperReviews.size();i++) {
							try {
								File file = my_frame.chooseFileSave();
								System.out.print(paperReviews.get(i));
								CopyDocuments.copy(new File("Reviews/"+paperReviews.get(i)), file);
							} catch (IOException e1) {
								e1.printStackTrace();
							}
						}
					}
				}
			}
		});
	}
}
